package ru.spb.osll.myfit.server.execute;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLClassLoader;
import java.util.List;

import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.server.MyFitClassLoaderFactory;
import ru.spb.osll.myfit.server.fixtureiterator.FileFactory;
import ru.spb.osll.myfit.server.parser.TableParser;
import ru.spb.osll.myfit.util.IOUtils;
import ru.spb.osll.myfit.util.StringUtils;

public class ConsoleTestRunner {
	private String getTemplate(String fileName) throws IOException {
		FileInputStream stream = new FileInputStream(fileName);
		List<String> lines = IOUtils.readLines(new BufferedReader(
				new InputStreamReader(stream)));
		stream.close();
		return StringUtils.join(lines, "\n");
	}

	public void run(String[] args) throws Exception {
		if (args.length != 2) {
			System.out
					.println("Usage: java ConsoleTestRunner <path/to/FitNesseRoot> <TestSuite.TestPage>");
		} else {
			URLClassLoader classLoader = new MyFitClassLoaderFactory(
					new FileFactory()).createClassLoader(
					"distr/apache-tomcat-7.0.2/bin/lib/deps",
					"distr/apache-tomcat-7.0.2/bin/lib/fixtures");
			File fitNesseRoot = new File(args[0]).getAbsoluteFile();
			MyFitFixtureListener listener = new MyFitFixtureListener();
			FitNesseTestRunner runner = new FitNesseTestRunner(fitNesseRoot
					.getParent(), fitNesseRoot.getName(),
					new MyFitFixtureLoader(classLoader), listener);
			runner.run(args[1]);
			String result = listener.getOutput().replace("&nbsp;", "");
			String header = getTemplate("etc/testResultHeader.template");
			String footer = getTemplate("etc/testResultFooter.template");
			FileOutputStream stream = new FileOutputStream(args[1] + ".html");
			stream.write(header.getBytes());
			stream.write(result.getBytes());
			stream.write(footer.getBytes());
			stream.close();
		}
	}

	public void run2(String[] args) throws Exception {
		if (args.length != 2) {
			System.out
					.println("Usage: java ConsoleTestRunner <path/to/FitNesseRoot> <TestSuite.TestPage>");
		} else {
			URLClassLoader classLoader = new MyFitClassLoaderFactory(
					new FileFactory()).createClassLoader(
					"distr/apache-tomcat-7.0.2/bin/lib/deps",
					"distr/apache-tomcat-7.0.2/bin/lib/fixtures");

			File fitNesseRoot = new File(args[0]).getAbsoluteFile();

			FixtureListenerImpl listener = new FixtureListenerImpl(
					new TableParser());
			FitNesseTestRunner runner = new FitNesseTestRunner(fitNesseRoot
					.getParent(), fitNesseRoot.getName(),
					new MyFitFixtureLoader(classLoader), listener);
			PageExecutor executor = new PageExecutor(runner, args[1], listener, null);
			executor.run();
			Page testResult = new Page();

			for (Table table : listener.getTables()) {
				testResult.getElements().add(table);
			}
			System.out.println("Done");
		}
	}

	public static void main(String[] args) throws Exception {
		ConsoleTestRunner runner = new ConsoleTestRunner();
		runner.run(args);
	}
}
